package demo.example.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import demo.example.common.PageRequest;
import demo.example.common.PageResult;
import demo.example.common.PageUtils;
import demo.example.model.dao.MessageMapper;
import demo.example.model.pojo.Message;
import demo.example.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class MessageServiceImpl  implements MessageService {

    @Autowired
    MessageMapper messageMapper;



    @Override
    public Message getMessage(String phone) {
        Message message = messageMapper.selectByPhone(phone);
        return message;
    }

    @Override
    public int insertMessage(Message record) {
        int result = messageMapper.insert(record);
        return result;
    }
    @Override
    public List<Message> getAllMessage() {
        List<Message> all = messageMapper.selectAll();
        return all;
    }

    @Override
    public List<Message> getAllMessage(Message message) {
        List<Message> all = messageMapper.selectByMessage(message);
        return all;
    }

    @Override
    public int deleteByPhone(String phone) {
        messageMapper.deleteByPhone(phone);

        return 0;
    }


    @Override
    public PageResult selectAllMessageByPage(PageRequest pageRequest) {
        return PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest));
    }

    @Override
    public PageResult selectAllByMessage(Message message, PageRequest pageRequest) {
        return PageUtils.getPageResult(pageRequest, selectPageInfo(message,pageRequest));
    }

    /**
     * 调用分页插件完成分页
     * @param
     * @return
     */
    private PageInfo<Message> getPageInfo(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<Message> sysMenus = messageMapper.selectAll();
        return new PageInfo<Message>(sysMenus);
    }

    private PageInfo<Message> selectPageInfo(Message message,PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<Message> sysMenus = messageMapper.selectByMessage(message);
        return new PageInfo<Message>(sysMenus);
    }



}
